package com.syx.judge.Callback;

import cn.hutool.core.util.StrUtil;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.StreamType;
import com.github.dockerjava.core.command.ExecStartResultCallback;
import com.syx.commoncore.enums.CodeRunStatus;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

@Getter
@Setter
@Slf4j
public class DockerStartResultCallback extends ExecStartResultCallback {


    private CodeRunStatus codeRunStatus;  //记录执行成功还是失败
    //通过onNext方法将输出内容全部放到errorMessage中
    private String errorMessage;


    private String message;

    @Override
    public void onNext(Frame frame) {
        //将输出追加到errormessage
        StreamType streamType = frame.getStreamType();
        if (StreamType.STDERR.equals(streamType)) {
            if (StrUtil.isEmpty(errorMessage)) {

                errorMessage = new String(frame.getPayload());
            } else {

                errorMessage = errorMessage + new String(frame.getPayload());
            }
            codeRunStatus = CodeRunStatus.FAILED;
        } else {
            String msgTmp = new String(frame.getPayload());
            if (StrUtil.isNotEmpty(msgTmp)) {
                message = new String(frame.getPayload());
            }
            codeRunStatus = CodeRunStatus.SUCCEED;
        }
        super.onNext(frame);
    }
}
